local skel = fk.CreateSkill {
  name = "lb__yuanding",
  tags = { Skill.Compulsory }
}

Fk:loadTranslationTable {
  ["lb__yuanding"] = "鸳鼎",
  --[":lb__yuanding"] = "<b>锁定技</b>，你没有判定阶段且判定区中的红色/黑色牌称为“辣”/“清”，当“辣”数量为最多时，你的【杀】视为火【杀】且造成的伤害视为火焰伤害。",
  --10.25
  [":lb__yuanding"] = "<b>锁定技</b>，你没有判定阶段且判定区中的红色/黑色牌称为“辣”/“清”，当“辣”数量为最多时，你可将【杀】当火【杀】使用或打出。",

  ["@$lb__yuanding_red"] = "辣",
  ["@$lb__yuanding_black"] = "清",
  ["#lb__yuanding_log"] = "%arg：%from的%card视为火【杀】",
  ["#lb__yuanding"] = "鸳鼎：你可以将一张【杀】当火【杀】使用或打出",

  ["$lb__yuanding1"] = "我还是更喜欢火上浇油。",
  ["$lb__yuanding2"] = "文火慢熬，抑或武火爆炒？",
}

skel:addEffect("viewas", {
  anim_type = "offensive",
  pattern = "slash",
  prompt = "#lb__yuanding",
  handly_pile = true,
  filter_pattern = function(self, player, card_name)
    return {
      max_num = 1,
      min_num = 1,
      pattern = "slash",
    }
  end,
  view_as = function(self, player, cards)
    if #cards ~= 1 then return end
    local c = Fk:cloneCard("fire__slash")
    c.skillName = skel.name
    c:addSubcard(cards[1])
    return c
  end,
  enabled_at_play = function(self, player)
    local black, red = 0, 0
    for _, id in ipairs(player:getCardIds("j")) do
      local card = Fk:getCardById(id)
      if card.color == Card.Red then
        red = red + 1
      elseif card.color == Card.Black then
        black = black + 1
      end
    end
    return red > black
  end,
})

skel:addEffect(fk.EventPhaseChanging, {
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and Player.Judge == data.phase
  end,
  on_use = function(self, event, target, player, data)
    data.skipped = true
  end,
})

skel:addAcquireEffect(function(self, player, is_start)
  for _, color in ipairs({ Card.Red, Card.Black }) do
    local ids = table.filter(player:getCardIds("j"), function(id)
      return Fk:getCardById(id).color == color
    end)
    player.room:setPlayerMark(player, "@$lb__yuanding_" .. (color == Card.Red and "red" or "black"),
      #ids == 0 and 0 or ids)
  end
end)

skel:addLoseEffect(function(self, player, is_death)
  player.room:setPlayerMark(player, "@$lb__yuanding_red", 0)
  player.room:setPlayerMark(player, "@$lb__yuanding_black", 0)
end)
--[[
skel:addEffect(fk.AfterCardUseDeclared, {
  anim_type = "offensive",
  can_trigger = function(self, _, target, player, data)
    if player:hasSkill("lb__yuanding") and player == target and data.card.trueName == "slash" and data.card.name ~= "fire__slash" then
      local black,red = 0,0
      for _, id in ipairs(player:getCardIds("j")) do
        local card = Fk:getCardById(id)
        if card.color == Card.Red then
          red = red + 1
        elseif card.color == Card.Black then
          black = black + 1 end
      end
      return red > black
    end
  end,
  on_use = function (self, event, target, player, data)
    data:changeCard("fire__slash", data.card.suit, data.card.number, skel.name)
    player.room:sendLog{
      type = "#lb__yuanding_log",
      from = player.id,
      card = {data.card.id},
      arg = "lb__yuanding",
    }
  end,
})

skel:addEffect(fk.PreDamage, {
  anim_type = "offensive",
  can_trigger = function(self, _, target, player, data)
    if player:hasSkill("lb__yuanding") and player == target and data.damageType ~= fk.FireDamage then
      local black,red = {},{}
      for _, id in ipairs(player:getCardIds("j")) do
        local card = Fk:getCardById(id)
        if card.color == Card.Red then table.insert(red,id) end
        if card.color == Card.Black then table.insert(black,id) end
      end
      return #red > #black
    end
  end,
  on_use = function (self, event, target, player, data)
    data.damageType = fk.FireDamage
  end,
})
--]]
skel:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if not player:hasSkill("lb__yuanding", true, true) then return false end
    for _, move in ipairs(data) do
      return (move.from and move.from == player and table.find(move.moveInfo, function(info)
        return info.fromArea == Player.Judge
      end)) or (move.to and move.to == player and move.toArea == Player.Judge)
    end
  end,
  on_refresh = function(self, event, target, player, data)
    skel:onAcquire(player, false)
  end,
})

return skel
